Imports System.Drawing
Imports System.Drawing.Drawing2D

Namespace Annotations
  ''' <summary>
  ''' The strongly typed collection of masks
  ''' </summary>
  ''' <remarks>
  ''' </remarks>
  Public Class Masks
    Inherits CollectionWithEventsTemplate(Of Mask)

    ''' <summary>
    ''' Create a new mask collection
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()
    End Sub 'New

    ''' <summary>
    ''' Return the mask of the collection at the given index
    ''' </summary>
    ''' <param name="index"></param>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Mask(ByVal index As Integer) As Mask
      Get
        Return MyBase.Item(index)
      End Get
    End Property

    ''' <summary>
    ''' Return the mask of the collection at the given index
    ''' </summary>
    ''' <param name="key "></param>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Mask(ByVal key As String) As Mask
      Get
        Return MyBase.Item(Key)
      End Get
      Set(ByVal value As Mask)
        MyBase.Item(Key) = value
      End Set
    End Property

    ''' <summary>
    ''' Add a annotation to the collection, and connect event handlers to it.
    ''' </summary>
    ''' <param name="key"></param>
    ''' <param name="value"></param>
    ''' <remarks></remarks>
    Public Overrides Sub Add(ByVal key As String, ByVal value As Mask)
      value._key = key
      If MyBase.ContainsKey(key) Then MyBase.Remove(key)
      AddHandler value.SelectedChanged, AddressOf OnSelectedChangedEvent
      AddHandler value.Changed, AddressOf OnChangedEvent
      MyBase.Add(key, value)
    End Sub


#If IMAGEPROCESSING Then

    ''' <summary>
    ''' Add a mask of a certain size, derived from an annotation container, to the collection. All pixels inside a region
    ''' defined by this annotation container will be set to true, others will be false.
    ''' </summary>
    ''' <param name="key"></param>
    ''' <param name="container"></param>
    ''' <param name="width"></param>
    ''' <param name="heigth"></param>
    ''' <remarks></remarks>
    Public Overloads Sub Add(ByVal key As String, ByVal container As AnnotationContainer, ByVal width As Integer, ByVal heigth As Integer)
      Me.Add(key, CIPF.Annotations.Mask.FromAnnotationContainer(container, width, heigth))
    End Sub

    ''' <summary>
    ''' Add a mask of a certain size, derived from an annotation, to the collection. All pixels inside a region
    ''' defined by this annotation container will be set to true, others will be false.
    ''' </summary>
    ''' <param name="ann"></param>
    ''' <param name="width"></param>
    ''' <param name="heigth"></param>
    ''' <param name="key"></param>
    ''' <remarks></remarks>
    Public Overloads Sub Add(ByVal ann As Annotation, ByVal width As Integer, ByVal heigth As Integer, ByVal key As String)
      Me.Add(key, CIPF.Annotations.Mask.FromAnnotation(ann, width, heigth))
    End Sub

    ''' <summary>
    ''' Add a mask of given size, derived from a region, to the collection
    ''' </summary>
    ''' <param name="region"></param>
    ''' <param name="width"></param>
    ''' <param name="heigth"></param>
    ''' <param name="foreground"></param>
    ''' <param name="key"></param>
    ''' <remarks></remarks>
    Public Overloads Sub Add(ByVal region As Region, ByVal width As Integer, ByVal heigth As Integer, ByVal foreground As Color, ByVal key As String)
      Me.Add(key, CIPF.Annotations.Mask.FromRegion(region, width, heigth, foreground))
    End Sub
#End If

    Public Sub Draw(ByVal g As Graphics, ByVal bitmap As Bitmap, ByVal srcRect As Rectangle, ByVal dstRect As Rectangle)
      'Source and destination rectangle are for the dimensions of the original image. 
      'Mask images can be undersampled compared to this!
      If Me.Visible Then
        For Each mask As Mask In Me
          If mask.Bitmap.Width <> bitmap.Width Then
            Dim underSampleFactor As Double = mask.Bitmap.Width / bitmap.Width
            Dim maskSrcRect As New Rectangle(CInt(Math.Round(srcRect.X * underSampleFactor)), CInt(Math.Round(srcRect.Y * underSampleFactor)), _
              CInt(Math.Round(srcRect.Width * underSampleFactor)), CInt(Math.Round(srcRect.Height * underSampleFactor)))
            mask.Draw(g, maskSrcRect, dstRect)
          Else
            mask.Draw(g, srcRect, dstRect)
          End If
        Next
      End If
    End Sub
  End Class
End Namespace
